package com.wanxi.servlet.user;

import com.alibaba.fastjson.JSONObject;
import com.wanxi.dto.ResultDTO;
import com.wanxi.vo.UserVO;
import com.wanxi.service.UserService;
import com.wanxi.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

@WebServlet("/back/user/delete")
public class UserDeleteServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 1.乱码
        req.setCharacterEncoding(StandardCharsets.UTF_8.toString());
        resp.setCharacterEncoding(StandardCharsets.UTF_8.toString());
//        2.得到前端的值
        String idStr = req.getParameter("id");
        int id = 0;
       ResultDTO resultDTO=null;
        try {
            id = Integer.parseInt(idStr);

//        3.封装VO数据
            UserVO userVO = new UserVO();
            userVO.setId(id);
//        4.调用服务逻辑层
            UserService userService = new UserServiceImpl();
//        5.得到返回的结果
            resultDTO = userService.delete(userVO);
        } catch (Exception e) {
            e.printStackTrace();
        }

//        6.返回给前端

        resp.getWriter().println(JSONObject.toJSONString(resultDTO));
    }
}
